package com.bilibili.controller;

import com.bilibili.annocation.CurUser;
import com.bilibili.entity.User;
import com.bilibili.exception.BusinessException;
import com.bilibili.service.UserService;
import com.bilibili.utils.CookieOperations;
import com.bilibili.utils.RandomUtils;
import com.bilibili.utils.RedisOperations;
import com.bilibili.vo.UserLoginVo;
import com.bilibili.vo.UserRegistVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import static com.bilibili.constant.BusinessConstant.*;

/**
 * 用户相关的 controller接口
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private CookieOperations cookieOperations;
    @Autowired
    private RedisOperations redisOperations;

    /**
     * 判断用户名是否存在
     *
     * @param userName 用户名
     * @return 是否存在
     */
    @RequestMapping(value = "users", method = RequestMethod.GET, params = {"userName"})
    public boolean isUserNameExists(@RequestParam("userName") String userName) {
        return userService.isUserNameExists(userName);
    }

    /**
     * 判断手机号是否存在
     * @param phone 手机号
     * @return 是否存在
     */
    @RequestMapping(value="users",method = RequestMethod.GET,params = {"phone"})
    public boolean isPhoneExist(@RequestParam("phone") String phone){
        return userService.isPhoneExists(phone);
    }

    /**
     * 给手机发送短信验证码
     * @param phone 要发送短信的哪个手机号
     */
    @RequestMapping(value = "sendValidateCode", method = RequestMethod.POST)
    public boolean sendValidateCode(@RequestParam("phone") String phone) {
        return userService.sendValidateCode(phone);
    }
    /**
     * 注册操作
     * @param userRegistVo 注册操作时前端传递的参数
     * @return 注册成功后的用户信息
     */
    @RequestMapping(value = "regist", method = RequestMethod.POST)
    public User regist(@RequestBody UserRegistVo userRegistVo) {
        return userService.regist(userRegistVo);
    }

    /**
     * 登录操作
     * @param userLoginVo 用户登录时候的参数封装的对象
     * @return 登录成功的用户信息
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public User login(@RequestBody UserLoginVo userLoginVo) {
        User user = userService.login(userLoginVo);
        //a.生成一个用于保存在cookie中的凭证
        String token = RandomUtils.generateUuid();
        //b.将这个token保存在cookie中
        cookieOperations.set(COOKIE_TOKEN_NAME, token, USER_TOKEN_MAX_AGE);
        //c.在redis中存储当前这个token对应的客户端的用户登录信息
        String redisUserKey = REDIS_USER_PREFIX + token;
        redisOperations.set(redisUserKey, user);
        redisOperations.expire(redisUserKey, USER_TOKEN_MAX_AGE);
        return user;
    }

    /**
     * 获取当前登录用户
     * @return 登录成功的用户信息
     */
    @RequestMapping(value = "loginUser", method = RequestMethod.GET)
    public User getLoginUser(@CurUser User user) {
        return user;
    }

    /**
     * 退出登录操作
     * @return 是否登出成功
     */
    @RequestMapping(value = "logout", method = RequestMethod.POST)
    public boolean logout() {
        String token = cookieOperations.get(COOKIE_TOKEN_NAME);
        if (StringUtils.isBlank(token)) {
            throw new BusinessException(HttpStatus.UNAUTHORIZED, "用户已经退出登录");
        } else {
            //删除Cookie中对应的token
            cookieOperations.del(COOKIE_TOKEN_NAME);
            //删除token在redis中对应key
            redisOperations.del(REDIS_USER_PREFIX + token);
        }
        return true;
    }
}
